package com.tenglong.teacher.ui.homeworkNew;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.Utils;
import com.cuotibao.zhuoxiao.basics.utils.recyview.DividerSpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.BaseHomeWorkInfo;
import com.tenglong.teacher.data.resultInfo.HistoryHomeWorkClassHourInfo;
import com.tenglong.teacher.data.resultInfo.HistoryHomeWorkOutsideInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.HomeWorkAdapter;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeworkClassHourActivity extends BaseActivity implements HomeWorkAdapter.HomeworkListener, OnRefreshListener {
    public static final String KEY_HOMEWORK_ID = "key_homework_id";
    public static final String KEY_CLASS_ID = "key_class_id";
    public static final String KEY_CLASS_NAME = "key_class_name";
    public static final String KEY_CLASS_DATA = "key_class_data";
    private RecyclerView mRecyclerView;
    private List<BaseHomeWorkInfo> infos;
    private HomeWorkAdapter mAdapter;
    private HistoryHomeWorkOutsideInfo data;
    private View noData;
    private SmartRefreshLayout mSmartRefreshLayout;
    private boolean isFistdownLoad = true;
    private String tag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homework_class_hour);
        data = (HistoryHomeWorkOutsideInfo) getIntent().getSerializableExtra(KEY_CLASS_DATA);
        tag=getIntent().getStringExtra("tag");
        initView();
        initRecy();
        setAdapter();
        setLisnters();
        requestData();
    }

    private void setLisnters() {
        mAdapter.setHomeworkListener(this);
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(this);
    }

    private void requestData() {
        if (isFistdownLoad) {
            showProgressDialog("数据加载中...");
            isFistdownLoad = false;
        }
        if (tag.equals(HomeWorkFragment.CURRENT_HOMEWORK_TAG)) {
            getNewList();
        } else {
            getOldList();
        }
    }
    private void getOldList(){
        CreateRequestEntity.getWebService().getHistoryHomeworkClassHourData(data.id).enqueue(new Callback<BaseResult<List<HistoryHomeWorkClassHourInfo>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<HistoryHomeWorkClassHourInfo>>> call, Response<BaseResult<List<HistoryHomeWorkClassHourInfo>>> response) {
                mSmartRefreshLayout.finishRefresh(true);
                if (ResultVerifier.isSucceed(response)) {
                    infos.clear();
                    infos.addAll(response.body().data);
                    mAdapter.setOld(true);
                    mAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
                noData.setVisibility(infos.size() == 0 ? View.VISIBLE : View.GONE);
                dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseResult<List<HistoryHomeWorkClassHourInfo>>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
                noData.setVisibility(infos.size() == 0 ? View.VISIBLE : View.GONE);
                mSmartRefreshLayout.finishRefresh(false);
                dismissProgressDialog();
            }
        });
    }
    private void getNewList(){
        CreateRequestEntity.getWebService().getNewHomeworkClassHourData(data.id).enqueue(new Callback<BaseResult<List<HistoryHomeWorkClassHourInfo>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<HistoryHomeWorkClassHourInfo>>> call, Response<BaseResult<List<HistoryHomeWorkClassHourInfo>>> response) {
                mSmartRefreshLayout.finishRefresh(true);
                if (ResultVerifier.isSucceed(response)) {
                    infos.clear();
                    infos.addAll(response.body().data);
                    mAdapter.setOld(false);
                    mAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
                noData.setVisibility(infos.size() == 0 ? View.VISIBLE : View.GONE);
                dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseResult<List<HistoryHomeWorkClassHourInfo>>> call, Throwable t) {
                ResultVerifier.systerErrers(t);
                noData.setVisibility(infos.size() == 0 ? View.VISIBLE : View.GONE);
                mSmartRefreshLayout.finishRefresh(false);
                dismissProgressDialog();
            }
        });
    }
    private void setAdapter() {
        infos = new ArrayList<>();
        mAdapter = new HomeWorkAdapter(infos);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initRecy() {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerSpaceItemDecoration(Utils.dip2px(this, 15), Utils.dip2px(this, 15)));
    }

    private void initView() {
        setTitle(data.name);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        noData = findViewById(R.id.noData);
        mSmartRefreshLayout = findViewById(R.id.smartRefreshLayout);
        ClassicsHeader headerView = new ClassicsHeader(this);
        ClassicsFooter footView = new ClassicsFooter(this);
        headerView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        footView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        mSmartRefreshLayout.setRefreshHeader(headerView);
        mSmartRefreshLayout.setRefreshFooter(footView);
    }

    @Override
    public void onClickCurrentItemListener(int position) {

    }

    @Override
    public void onClickHistoryItemListener(int position) {

    }

    @Override
    public void onClickHistoryClassHourItemListener(int position) {
        if (position == -1)return;
        HistoryHomeWorkClassHourInfo info = (HistoryHomeWorkClassHourInfo) infos.get(position);
        Intent intent = new Intent(this, HomeworkStudentListActivity.class);
        intent.putExtra(KEY_HOMEWORK_ID, info.homeworkid);
        intent.putExtra(KEY_CLASS_ID, info.toClasses);
        intent.putExtra(HomeworkStudentListActivity.KEY_CLASS_TOTAL_STUDENT_NUMBER, info.stuNum);
        intent.putExtra(KEY_CLASS_NAME, info.className);
        startActivity(intent);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        requestData();
    }
}
